package etxt2db.main;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;

public class tagCleaner {

	/**
	 * @param args
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws InterruptedException, IOException {
		
		int[] split = {0,1,2,3,4,5,6,7,8,9};
		
		for (int i = 0; i < split.length; i++) {
		
			String dataFolderName = "/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/" + i + "/taggedtestingDirectory/";
			
			String[] removableTags = {"LOCATION"};
			
			File dataFolder = new File(dataFolderName);
			
			File[] files = dataFolder.listFiles();
			
			String destinyFolderName = "/home/pjbarrio/Dataset/NaturalDisaster/SimpleData/" + i + "/testingDirectory/idealResults/";
			
			new File(destinyFolderName).mkdir();
			
			for (File file : files) {
				
				cleanTags(file,removableTags,new File(destinyFolderName + file.getName()));
							
			}

		}
		
	}

	public static void cleanTags(File file, String[] removableTags, File destinyFile) throws IOException {
		
		String fileText = readFile(file);
		
		fileText = cleanText(fileText,removableTags);
		
		saveCleanText(fileText,destinyFile);

		
	}

	private static void saveCleanText(String fileText, File file) throws IOException {
		
		BufferedWriter bw = new BufferedWriter(new FileWriter(file));
		
		bw.write(fileText);
		
		bw.close();
		
	}

	private static String cleanText(String fileText, String[] removableTags) {
		
		String ret = fileText;
		
		//Reduce many Spaces to one
		
		ret = fileText.replaceAll(" +", " ");
		
		//removeTags
		
		for (int i = 0; i < removableTags.length; i++) {
			
			ret = removeTags(ret,removableTags[i]);
			
		}
		
		
		return ret;
		
	}

	private static String removeTags(String fileText, String tag) {
		
		fileText = fileText.replaceAll(generateOpenTag(tag), "");
		
		fileText = fileText.replaceAll(generateCloseTag(tag), "");
		
		return fileText;
		
	}

	private static String generateCloseTag(String tag) {
		
		return new String("<" + tag + ">");
	
	}

	private static String generateOpenTag(String tag) {
		
		return new String("</" + tag + ">");
		
	}

	private static String readFile(File file) {
		
	    byte[] buffer = new byte[(int) file.length()];
	    BufferedInputStream f = null;
	    try {
	        f = new BufferedInputStream(new FileInputStream(file.getAbsolutePath()));
	        f.read(buffer);
	    } catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
	        if (f != null) try { f.close(); } catch (IOException ignored) { }
	    }
	    return new String(buffer);
		
	}

}
